package com.seaengine.net
{
	import flash.utils.ByteArray;

	public class Common
	{
		public function Common()
		{
		}
		
		/**
		 * 
		 * @param src
		 * @return 
		 * 
		 */
		public static function toIntArray(src:*):Array
		{
			var res:Array = [];
			if(src is ByteArray)
			{
				src.position = 0;
				for(var j:int=0;j< src.length; ++j)
				{
					res.push(src.readByte());
				}
			}else if(src is String){
				for(var i:int= 0; i< src.length ; ++i)  
				{
					res.push(src.charCodeAt(i));
				}
			}else if (src is Array)
			{
				res = src;
			}
			return res;
		}
		
		/**
		 * 
		 * @param src
		 * @param dst
		 * 
		 */
		public static function deepCopyArray(src:Array,dst:Array):void
		{
			while(dst.length)
			{
				dst.pop();
			}
			for(var i:uint = 0 ;i < src.length ;++i)
			{
				dst.push(src[i]);
			}
		}
		
		/**
		 * 
		 * @param buf
		 * @param ar
		 * 
		 */
		public static function paddingInt(buf:ByteArray,ar:Array):void
		{
			buf.writeInt(ar[0]);
			buf.writeInt(ar[1]);
		}
		
		/**
		 * 
		 * @param src
		 * @return 
		 * 
		 */
		public static function htons(src:int):int
		{
			/*var a:Buffer = new Buffer();
			a.writeShort(src);
			a.seek();
			var b:ByteArray = new ByteArray();
			b.writeBytes(a.rawByteArray);
			b.position = 0;
			return b.readShort();*/
			
			//effective
			
			//fixme:
			return src;
			
			//return	((src & 0x00ff) << 8) | ((src & 0xff00) >>> 8) ;
		}
		
		/**
		 * 
		 * @param src
		 * @return 
		 * 
		 */
		public static function ntohs(src:int):int
		{
			/*var a:ByteArray = new ByteArray();
			a.writeShort(src);
			a.position = 0;
			var b:Buffer = new Buffer();
			b.writeBytes(a);
			b.seek();
			return b.readShort();*/
			//return (0 | (src << 8) | (src >>> 24) );
			
			return src;
			//return	((src & 0x00ff) << 8) | ((src & 0xff00) >>> 8) ;
			
			
		}
		
		/**
		 * 
		 * @param src
		 * @return 
		 * 
		 */
		public static function ntohl(src:int):int
		{
			/*var a:ByteArray = new ByteArray();
			a.writeInt(src);
			a.position = 0;
			var b:Buffer = new Buffer();
			b.writeBytes(a);
			b.seek();
			return b.readInt();*/
			return src;
			//return ((0x000000ff & src) << 24 ) | ((0x0000ff00 & src ) << 8) | ((0x00ff0000 & src ) >>> 8) | ((0xff000000 & src) >>> 24);
		}
		
		/**
		 * 
		 * @param src
		 * @return 
		 * 
		 */
		public static function htonl(src:int):int
		{
			/*var a:Buffer = new Buffer();
			a.writeInt(src);
			a.seek();
			var b:ByteArray = new ByteArray();
			b.writeBytes(a.rawByteArray);
			b.position = 0;
			return b.readInt();*/
			return src;
			//return ((0x000000ff & src) << 24 ) | ((0x0000ff00 & src ) << 8) | ((0x00ff0000 & src ) >>> 8) | ((0xff000000 & src) >>> 24);
		}
		
		/**
		 * 
		 * @param s
		 * @return 
		 * 
		 */
		public static function stringToUnicode(s:String):String
		{
			var b:ByteArray = new ByteArray();
			b.writeMultiByte(s,"unicode");
			b.position = 0;
			return b.readMultiByte(b.bytesAvailable,"us-ascii");
		}
		
		/**
		 * 
		 * @param s
		 * @return 
		 * 
		 */
		public static function stringToUTF8(s:String):String
		{
			var b:ByteArray = new ByteArray();
			b.writeUTFBytes(s);
			b.position = 0;
			return b.readMultiByte(b.bytesAvailable,"us-ascii");
		}
		
		/**
		 *反序列化16进制字符串 
		 * @param s
		 * @return 
		 * 
		 */
		public static function hexStringToBuffer(s:String):JoyPortBuffer
		{
			if (!s || s.length%2 != 0 )
				return null;
			
			var ret:JoyPortBuffer = new JoyPortBuffer();
			
			for (var i:int = 0 ; i < s.length/2 ; ++i)
			{
				ret.encodeInt8( parseInt(s.substr(i*2,2),16) );
			}

			return ret;
		}
		
		/**
		 * 序列化字节流成16进制字符串
		 * @param b
		 * @return 
		 * 
		 */
		public static function bufferToHexString(b:Buffer):String
		{
			if ( !b )
				return null;
			
			b.position = 0;
			var result:String = "";
			while ( b.bytesAvailable )
			{
				var s:String = b.readByte().toString(16);
				result += (s.length == 1)?"0"+s:s;
			}
			return result;
		}
		
		public static function removeTag(src:String):String
		{
			/*var data:XML ; 
			var result:String = "";
			try 
			{
				data = XML(src);
			}catch(error:Error){
				return src;
			}
			if ( !data )
				return src;
			if ( data && data is XML)
			{
				for each( var section:XML in data..t)
				{
					result += section.text().toString();
				}
			}*/
			return src;
		}
		
		public static function debug(s:ByteArray,r:uint=16):void
		{
			if(!s)
			{
				return;
			}
			var rawPos:int = s.position;
			s.position = 0;
			var res:String = "";
			for(var i:int=0;i<s.length;++i)
			{
				var str:String = s[i].toString(r);
				
				str = (str.length == 1)?"0"+str:str;
				res += "0x"+str + "/";
				if(i%17 == 0 && i != 0)
				{
					res += "\n";
				}
			}
			s.position = rawPos;
		}
	}	
}